﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Xml;
using HtmlAgilityPack;
using System.Windows.Media.Imaging;



namespace MolfMolfMolf
{
    public partial class NewsItem : UserControl
    {
        public string uri { get; private set; }
        public string imgUri { get; private set; }
        public string newstitle { get; private set; }
        public string date { get; private set; }

        WebClient client = new WebClient();

        
        

        public NewsItem() 
        {
            InitializeComponent();
            
        }

        public NewsItem(string t,string d, string pubDate,string u )
        {
            InitializeComponent();
            title.Text = t;
            description.Text = d;
            updateDate.Text = pubDate;
            newstitle = t;
            date = pubDate;
            uri = u;

            //img
            HtmlWeb web = new HtmlWeb();
            web.LoadCompleted += new EventHandler<HtmlDocumentLoadCompleted>(web_LoadCompleted);
            web.LoadAsync(uri);
            image.Source = new BitmapImage(new Uri("Images/loading.png", UriKind.Relative));
         }

        void web_LoadCompleted(object sender, HtmlDocumentLoadCompleted e)
        {
            try
            {

                HtmlDocument document = e.Document;
                HtmlNodeCollection images = document.DocumentNode.SelectNodes("//div[@class='news-image']/img");
                this.imgUri = "http://www.golf.fi" + images[0].GetAttributeValue("src", "");
                image.Source = new BitmapImage(new Uri(imgUri, UriKind.Absolute));
            }
            catch
            {
                image.Source = new BitmapImage(new Uri("Images/default.jpg", UriKind.Relative));
            }
        }

        public NewsItem(string t, string d, string pubDate)
        {
            InitializeComponent();
            title.Text = t;
            description.Text = d;
            updateDate.Text = pubDate;
            date = pubDate;
            newstitle = t;
            
        }

        private void setTitle(string t)
        {
            title.Text = t;

        }
        private void setDes(string d)
        {
            description.Text = d;

        }
        private void setPublishDate(string l)
        {
            updateDate.Text = l;
        }
    }

    
}
